//
//  VideoSettingViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-12.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "VideoSettingViewController.h"
#import "VideoCodeSettingViewController.h"

@interface VideoSettingViewController ()

@end

@implementation VideoSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //Custom initialization
        self.title = NSLocalizedString(@"video settings", @"视频配置 Video settings");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.segment = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"QCIF",@"CIF",@"VGA"]];
    [self.segment setFrame:CGRectMake(130, 150, 170, 30)];
    [self.segment setSelectionIndicatorHeight:3.5f];                                                                                  
    [self.segment setBackgroundColor:[UIColor whiteColor]];
    [self.segment setTextColor:[UIColor colorWithRed:83/255.0 green:215/255.0 blue:105/255.0 alpha:1.0]];
    [self.segment setSelectionIndicatorColor:[UIColor colorWithRed:83/255.0 green:215/255.0 blue:105/255.0 alpha:1.0]];
    [self.segment setSelectionIndicatorMode:HMSelectionIndicatorFillsSegment];
    [self.segment addTarget:self action:@selector(segamentSelectChanged:) forControlEvents:UIControlEventValueChanged];
    [self.segment setSegmentEdgeInset:UIEdgeInsetsMake(0, 6, 0, 6)];
    [self.segment setFont:[UIFont systemFontOfSize:16.0f]];
    [self.view addSubview:self.segment];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *isOpenVideo = [[NSString alloc] init];
    isOpenVideo = [defaults objectForKey:@"SAVEISOPENVIDEO"];
    if (isOpenVideo.length > 0)
    {
        if ([isOpenVideo isEqualToString:@"YES"])
        {
            [self.switch1 setOn:YES];
        }
        else if ([isOpenVideo isEqualToString:@"NO"])
        {
            [self.switch1 setOn:NO];
        }
    }
    NSString *isOpenSelfVideo = [[NSString alloc] init];
    isOpenSelfVideo = [defaults objectForKey:@"SAVEISOPENSELFVIDEO"];
    if (isOpenSelfVideo.length > 0)
    {
        if ([isOpenSelfVideo isEqualToString:@"YES"])
        {
            [self.switch2 setOn:YES];
        }
        else if ([isOpenSelfVideo isEqualToString:@"NO"])
        {
            [self.switch2 setOn:NO];
        }
    }
    NSString *isUseFrontCamera = [[NSString alloc] init];
    isUseFrontCamera = [defaults objectForKey:@"SAVEFRONTCAMERA"];
    if (isUseFrontCamera.length > 0)
    {
        if ([isUseFrontCamera isEqualToString:@"YES"])
        {
            [self.switch3 setOn:YES];
        }
        else if ([isUseFrontCamera isEqualToString:@"NO"])
        {
            [self.switch3 setOn:NO];
        }
    }
    
    NSString *tempSegmented = [[NSString alloc] init];
    tempSegmented = [defaults objectForKey:@"SAVEVIDEOSIZE"];
    if (tempSegmented.length > 0)
    {
        if ([tempSegmented isEqualToString:@"QCIF"])
        {
            [self.segment setSelectedIndex:0];
        }
        else if ([tempSegmented isEqualToString:@"CIF"])
        {
            [self.segment setSelectedIndex:1];
        }
        else if ([tempSegmented isEqualToString:@"VGA"])
        {
            [self.segment setSelectedIndex:2];
        }
    }
}

- (IBAction)openVideoSwitch:(id)sender
{
    UISwitch *switchControl = (UISwitch *)sender;
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isOpenVideo = switchControl.on;
    if (isOpenVideo == YES)
    {
        [interface isOpenVideo:YES];
        [defaults setObject:@"YES" forKey:@"SAVEISOPENVIDEO"];
    }
    else
    {
        [interface isOpenVideo:NO];
        [defaults setObject:@"NO" forKey:@"SAVEISOPENVIDEO"];
    }
    [defaults synchronize];
}

- (IBAction)openVideoSelfSwhitch:(id)sender
{
    UISwitch *switchControl = (UISwitch *)sender;
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isOpenSelfVideo = switchControl.on;
    if (isOpenSelfVideo == YES)
    {
        [interface isOpenSelfVideo:YES];
        [defaults setObject:@"YES" forKey:@"SAVEISOPENSELFVIDEO"];
    }
    else
    {
        [interface isOpenSelfVideo:NO];
        [defaults setObject:@"NO" forKey:@"SAVEISOPENSELFVIDEO"];
    }
    [defaults synchronize];
}

- (IBAction)useFrontCameraSwitch:(id)sender
{
    UISwitch *switchControl = (UISwitch *)sender;
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isUseFrontCamera = switchControl.on;
    if (isUseFrontCamera == YES)
    {
        [interface FonrtDevice:1];
        [defaults setObject:@"YES" forKey:@"SAVEFRONTCAMERA"];
    }
    else
    {
        [interface FonrtDevice:0];
        [defaults setObject:@"NO" forKey:@"SAVEFRONTCAMERA"];
    }
    [defaults synchronize];
}

- (IBAction)segamentSelectChanged:(HMSegmentedControl *)segmentedControl
{
    
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (segmentedControl.selectedIndex == 0)
    {
        [interface videoSize:@"QCIF"];
        [defaults setObject:@"QCIF" forKey:@"SAVEVIDEOSIZE"];
    }
    else if (segmentedControl.selectedIndex == 1)
    {
        [interface videoSize:@"CIF"];
        [defaults setObject:@"CIF" forKey:@"SAVEVIDEOSIZE"];
    }
    else if (segmentedControl.selectedIndex == 2)
    {
        [interface videoSize:@"VGA"];
        [defaults setObject:@"VGA" forKey:@"SAVEVIDEOSIZE"];
    }
    [defaults synchronize];
}

- (IBAction)VideoCodeOnButton:(id)sender
{
    VideoCodeSettingViewController *videoCodeSettingVC = [[VideoCodeSettingViewController alloc] init];
    [self.navigationController pushViewController:videoCodeSettingVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
